Page({
  data: {
    showsearch: false,   //显示搜索按钮
    searchtext: '',  //搜索文字
    filterdata: {},  //筛选条件数据
    showfilter: false, //是否显示下拉筛选
    showfilterindex: null, //显示哪个筛选类目
    sortindex: 0,  //一级分类索引
    sortid: null,  //一级分类id
    subsortindex: 0, //二级分类索引
    subsortid: null, //二级分类id
    cityindex: 0,  //一级城市索引
    cityid: null,  //一级城市id
    subcityindex: 0,  //二级城市索引
    subcityid: null, //二级城市id
    servicelist: [], //服务集市列表
    scrolltop: null, //滚动位置
    page: 0  //分页
  },
  onLoad: function () { //加载数据渲染页面
    this.fetchServiceData();
    this.fetchFilterData();
  },
  fetchFilterData: function () { //获取筛选条件
    this.setData({
      filterdata: {
        "sort": [
          {
            "id": 0,
            "title": "全部"
          },
          {
            "id": 1,
            "title": "政府政策",
            "subsort": [
              {
                "id": 1,
                "title": "全部"
              },
              {
                "id": 11,
                "title": "精准扶贫"
              },
              {
                "id": 12,
                "title": "两项补贴"
              },
              {
                "id": 13,
                "title": "针对性帮扶"
              },
            ]
          },
          {
            "id": 2,
            "title": "公益组织",
            "subsort": [
              {
                "id": 2,
                "title": "全部"
              },
              {
                "id": 21,
                "title": "中国红十字基金会"
              },
              {
                "id": 22,
                "title": "希望工程"
              },
              {
                "id": 23,
                "title": "中国志愿者协会"
              },
            ]
          },
          {
            "id": 3,
            "title": "慈善项目",
            "subsort": [
              {
                "id": 3,
                "title": "全部"
              },
              {
                "id": 31,
                "title": "项目编号"
              },
              {
                "id": 32,
                "title": "项目名称"
              }
            ]
          },
          {
            "id": 4,
            "title": "网络捐助",
            "subsort": [
              {
                "id": 4,
                "title": "全部"
              },
              {
                "id": 41,
                "title": "项目查询"
              },
              {
                "id": 42,
                "title": "受助对象查询"
              }
            ]
          },
        ],
        "city": [
          {
            "id": 0,
            "title": "全部"
          },
          {
            "id": 1,
            "title": "湖北省",
            "subcity": [
              {
                "id": 1,
                "title": "全部"
              },
              {
                "id": 11,
                "title": "武汉市"
              },
              {
                "id": 12,
                "title": "襄阳市"
              },
              {
                "id": 13,
                "title": "孝感市"
              },
              {
                "id": 14,
                "title": "随州市"
              },
              {
                "id": 15,
                "title": "荆州市"
              },
              {
                "id": 16,
                "title": "黄冈市"
              },
              {
                "id": 17,
                "title": "天门市"
              },
              {
                "id": 18,
                "title": "仙桃市"
              },
              {
                "id": 19,
                "title": "潜江市"
              },
              {
                "id": 20,
                "title": "十堰市"
              },
              {
                "id": 21,
                "title": "宜昌市"
              },
              {
                "id": 22,
                "title": "咸宁市"
              },
            ]
          },
          {
            "id": 2,
            "title": "浙江省",
            "subcity": [
              {
                "id": 2,
                "title": "全部"
              },
              {
                "id": 21,
                "title": "杭州市"
              },
              {
                "id": 22,
                "title": "金华市"
              },
              {
                "id": 23,
                "title": "义乌市"
              },
            ]
          },
          {
            "id": 3,
            "title": "江苏省",
            "subcity": [
              {
                "id": 3,
                "title": "全部"
              },
              {
                "id": 31,
                "title": "南京市"
              },
              {
                "id": 32,
                "title": "苏州市"
              },
            ]
          },
          {
            "id": 4,
            "title": "上海市",
            "subcity": [
              {
                "id": 4,
                "title": "全部"
              }
            ]
          },
          {
            "id": 5,
            "title": "云南省",
            "subcity": [
              {
                "id": 5,
                "title": "全部"
              }
            ]
          },
          {
            "id": 6,
            "title": "河南",
            "subcity": [
              {
                "id": 6,
                "title": "全部"
              },
              {
                "id": 61,
                "title": "郑州市"
              }
            ]
          }
        ],
      }
    })
  },
  fetchServiceData: function () {  //获取城市列表
    let _this = this;
    wx.showToast({
      title: '加载中',
      icon: 'loading'
    })
    const perpage = 10;
    this.setData({
      page: this.data.page + 1
    })
    const page = this.data.page;
    const newlist = [];
    for (var i = 1; i < 2; i++) {
      newlist.push({
        "id": 1,
        "name": "WWF China",
        "city": "上海市",
        "tag": "公益组织",
        "imgurl": "http://images.cdb.org.cn/201409/20140904/230x115/bf3fc2370509773a5df8c5773b76a24a.png"
      },
        {
          "id": 2,
          "name": "郑州永辉射线防护设备有限公司",
          "city": "郑州市",
          "tag": "公益组织",
          "imgurl": "http://images.cdb.org.cn/201907/20190718/230x115/2141398101d838f3bb4dfbe2cddf66c5.jpg"
        },
        {
          "id": 3,
          "name": "云南省社会组织促进会",
          "city": "云南省",
          "tag": "公益组织",
          "imgurl": "http://images.cdb.org.cn/201907/20190718/230x115/b075bec4765a2b271c793a45e427ac7c.png"
        },
        {
          "id": 4,
          "name": "联合国儿童基金会",
          "city": "全球",
          "tag": "公益组织",
          "imgurl": "http://static.unicef.cn/201904cws/images/banner1.jpg?1"
        },
        {
          "id": 5,
          "name": "武汉市扶贫开发办公室",
          "city": "武汉市",
          "tag": "精准扶贫",
          "imgurl": "http://5b0988e595225.cdn.sohucs.com/images/20190312/457bd836e1ed47c0b90d811667a68446.jpeg"
        })
    }
    setTimeout(() => {
      _this.setData({
        servicelist: _this.data.servicelist.concat(newlist)
      })
    }, 1500)
  },
  inputSearch: function (e) {  //输入搜索文字
    this.setData({
      showsearch: e.detail.cursor > 0,
      searchtext: e.detail.value
    })
  },
  submitSearch: function () {  //提交搜索
    console.log(this.data.searchtext);
    this.fetchServiceData();
  },
  setFilterPanel: function (e) { //展开筛选面板
    const d = this.data;
    const i = e.currentTarget.dataset.findex;
    if (d.showfilterindex == i) {
      this.setData({
        showfilter: false,
        showfilterindex: null
      })
    } else {
      this.setData({
        showfilter: true,
        showfilterindex: i,
      })
    }
    console.log(d.showfilterindex);
  },
  setSortIndex: function (e) { //服务类别一级索引
    const d = this.data;
    const dataset = e.currentTarget.dataset;
    this.setData({
      sortindex: dataset.sortindex,
      sortid: dataset.sortid,
      subsortindex: d.sortindex == dataset.sortindex ? d.subsortindex : 0
    })
    console.log('服务类别id：一级--' + this.data.sortid + ',二级--' + this.data.subsortid);
  },
  setSubsortIndex: function (e) { //服务类别二级索引
    const dataset = e.currentTarget.dataset;
    this.setData({
      subsortindex: dataset.subsortindex,
      subsortid: dataset.subsortid,
    })
    console.log('服务类别id：一级--' + this.data.sortid + ',二级--' + this.data.subsortid);
  },
  setCityIndex: function (e) { //服务城市一级索引
    const d = this.data;
    const dataset = e.currentTarget.dataset;
    this.setData({
      cityindex: dataset.cityindex,
      cityid: dataset.cityid,
      subcityindex: d.cityindex == dataset.cityindex ? d.subcityindex : 0
    })
    console.log('服务区id：一级--' + this.data.cityid + ',二级--' + this.data.subcityid);
  },
  setSubcityIndex: function (e) { //服务城市二级索引
    const dataset = e.currentTarget.dataset;
    this.setData({
      subcityindex: dataset.subcityindex,
      subcityid: dataset.subcityid,
    })
    console.log('服务区id：一级--' + this.data.cityid + ',二级--' + this.data.subcityid);
  },
  hideFilter: function () { //关闭筛选面板
    this.setData({
      showfilter: false,
      showfilterindex: null
    })
  },
  scrollHandle: function (e) { //滚动事件
    this.setData({
      scrolltop: e.detail.scrollTop
    })
  },
  goToTop: function () { //回到顶部
    this.setData({
      scrolltop: 0
    })
  },
  //scrollLoading:function(){ //滚动加载
  // this.fetchServiceData();
  // },
  onPullDownRefresh: function () { //下拉刷新
    this.setData({
      page: 0,
      servicelist: []
    })
    this.fetchServiceData();
    this.fetchFilterData();
    setTimeout(() => {
      wx.stopPullDownRefresh()
    }, 1000)
  }
})